home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / src / xvectest.cc < prev   
C/C++ Source or Header  |  1989-08-18  |  8KB  |  306 lines

  1. /*
  2.  *    Test of vector routines for type <T>
  3.  *
  4.  *    Copyright (C) 1988, 1989.
  5.  *
  6.  *    Dr. Thomas Keffer
  7.  *    Rogue Wave Associates
  8.  *    P.O. Box 85341
  9.  *    Seattle WA 98145-1341
  10.  *
  11.  *    Permission to use, copy, modify, and distribute this
  12.  *    software and its documentation for any purpose and
  13.  *    without fee is hereby granted, provided that the
  14.  *    above copyright notice appear in all copies and that
  15.  *    both that copyright notice and this permission notice
  16.  *    appear in supporting documentation.
  17.  *    
  18.  *    This software is provided "as is" without any
  19.  *    expressed or implied warranty.
  20.  *
  21.  *
  22.  *    @(#)xvectest.cc    2.1    8/18/89
  23.  */
  24.  
  25. /* 
  26. Note that in what follows, explicit type conversions have been used.
  27. For example,
  28.  
  29.     c(1) = <T>(-1.0);
  30.     c[2] = <T>(-2.0);
  31.  
  32. This is for the benefit of type complex.  It is not necessary to do
  33. this when using the GNU compiler and its class Complex (as declared in
  34. Complex.h).  However, it is necessary when using the AT&T class
  35. complex, as declared in complex.h.  I have not tried the GNU Complex.h
  36. file with the AT&T compiler.  
  37. */
  38.  
  39. #include "rw/<T>Vec.h"
  40. #include <stream.h>
  41.  
  42. #define TYPE <T>_TYPE
  43. #include "vecdefs.h"
  44.  
  45. main()
  46. {
  47.   {
  48.     cout << "\n**** Constructors / destructors ****\n";
  49.  
  50.     //  <T>Vec();
  51.     <T>Vec a;
  52.     cout << NL << "a:\n" << a <<NL;
  53.     
  54.     // <T>Vec(unsigned n);
  55.     <T>Vec* b = new <T>Vec(10);
  56.     cout << NL << "<T>Vec* b = new <T>Vec(10):\n"<< *b <<NL;
  57.     
  58.     // ~<T>Vec();
  59.     delete b;
  60.     
  61.     // <T>Vec(unsigned n, <T> val, <T> by);
  62.     <T>Vec c(15, <T>(5.0), <T>(1.0));
  63.     cout << NL << "<T>Vec c(15, <T>(5.0), <T>(1.0)):\n" << c <<NL;
  64.  
  65.     // <T>Vec(const <T>Vec& a);
  66.     <T>Vec d = c;
  67.     cout << NL << "<T>Vec d = c:\n"<<d <<NL;
  68.  
  69.     //void <T>Vec::deepenShallowCopy()
  70.     d.deepenShallowCopy();
  71.  
  72.     // <T>&        operator()(int i); // No bounds checking
  73.     // <T>&        operator[](int i); // With bounds checking
  74.     c(1) = <T>(-1.0);
  75.     c[2] = <T>(-2.0);
  76.     cout << NL << "c modified:\n" << c <<NL;
  77.     
  78.     cout << NL << "d.deepenShallowCopy():\n" << d <<NL;
  79.     
  80.     // <T>Vec        slice(int start, unsigned lgt, int strider=1);
  81.     // <T>Vec(const <T>Vec& a);
  82.     <T>Vec e = d.slice(0,5,1);
  83.     cout << NL << "<T>Vec e = d.slice(0,5,1):\n" << e <<NL;
  84.     
  85.     // <T>Vec(register unsigned n, const <T>* dat);
  86.     <T> some_data[8];
  87.     for(int i = 0; i<8; i++) some_data[i] = <T>(i);
  88.     <T>Vec f(some_data, 8);
  89.     cout << NL <<"<T>Vec f(8, some_data):\n" << f <<NL;
  90.     
  91.     // unsigned        length()    {return npts;}
  92.     cout << NL << "f.length(): " << f.length() << NL;
  93.  
  94.     // void <T>Vec::resize(unsigned)
  95.     a.resize(8);
  96.     cout << NL << "a.resize(8):\n" << a <<NL;
  97.  
  98.     // Assignment:
  99.     // <T>Vec        operator=(const <T>Vec&);
  100.     a = f;
  101.     cout << NL << "a = f:\n" << a <<NL;
  102.     
  103.     // <T>Vec        operator=(const <T>Vec&);
  104.     a.slice(1,3,2) = f.slice(0,3,2);
  105.     cout << NL << "a.slice(1,3,2) = f.slice(0,3,2):\n" << a <<NL;
  106.     
  107.     // <T>Vec        operator=(<T>);
  108.     a = <T>(1);
  109.     cout << NL << "a = <T>(1):\n" << a <<NL;
  110.     
  111.     f.slice(0,3,2) = <T>(0);
  112.     cout << NL << "f.slice(0,3,2) = <T>(0):\n" << f <<NL;
  113.     
  114.     //friend    <T>Vec    operator-(const <T>Vec&);
  115.     cout << NL << "-f\n" << -f <<NL;
  116.     
  117. #if HAS_INCRDECR
  118.     //friend    <T>Vec    operator++(<T>Vec&);
  119.     //friend    <T>Vec    operator--(<T>Vec&);
  120.     cout << NL << "f++\n" << f++ <<NL;
  121.     cout << NL << "f--\n" << f-- <<NL;
  122. #endif
  123.   }    
  124.  
  125.   // Begin operator section:
  126.   {
  127.     cout <<"\n**** Operators ****\n";
  128. #if TYPE==DComplex_TYPE
  129.     <T>Vec a(10, DComplex(1,-1));
  130.     <T>Vec b(10, DComplex(2,-2));
  131. #else
  132.     <T>Vec a(10, <T>(1));
  133.     <T>Vec b(10, <T>(2));
  134. #endif
  135.     cout << NL << "a:\n"<<a<<NL;
  136.     cout << NL << "b:\n"<<b<<NL;
  137. //friend    <T>Vec    operator*(const <T>Vec&,const <T>Vec&);
  138.     cout << NL << "a*b:\n" << a * b <<NL;
  139.  
  140. #if HAS_DIVIDE
  141. //friend    <T>Vec    operator/(const <T>Vec&,const <T>Vec&);
  142.     cout << NL << "a/b:\n" << a / b <<NL;
  143. #endif
  144.  
  145. //friend    <T>Vec    operator+(const <T>Vec&,const <T>Vec&);
  146.     cout << NL << "a+b:\n" << a + b <<NL;
  147.  
  148. //friend    <T>Vec    operator-(const <T>Vec&,const <T>Vec&);
  149.     cout << NL << "a-b:\n" << a - b <<NL;
  150.  
  151. //friend    <T>Vec    operator*(const <T>Vec&,<T>);
  152. //friend    <T>Vec    operator*(<T> s,const <T>Vec& V)
  153.     cout << NL << "4 * a * 4:\n" << <T>(4) * a * <T>(4) <<NL;
  154.  
  155. #if HAS_DIVIDE
  156. //friend    <T>Vec    operator/(const <T>Vec&,<T>);
  157.     cout << NL << "a / 4:\n" << a / <T>(4) <<NL;
  158.  
  159. //friend    <T>Vec    operator/(<T>,const <T>Vec&);
  160.     cout << NL << "4 / a:\n" << <T>(4) / a <<NL;
  161. #endif
  162.  
  163. //friend    <T>Vec    operator+(const <T>Vec&,<T>);
  164. //friend    <T>Vec    operator+(<T> s,const <T>Vec& V);
  165.     cout << NL << "4 + a + 4:\n" << <T>(4) + a + <T>(4) <<NL;
  166.  
  167. //friend    <T>Vec    operator-(const <T>Vec&,<T>);
  168.     cout << NL << "a - 4:\n" << a - <T>(4) <<NL;
  169. //friend    <T>Vec    operator-(<T>,const <T>Vec&);
  170.     cout << NL << "4 - a:\n" << <T>(4) - a <<NL;
  171.  
  172. //friend    void        operator+=(<T>Vec&,const <T>Vec&);
  173.     a += b;
  174.     cout << NL << "a += b:\n" << a <<NL;
  175.  
  176. //friend    void        operator+=(<T>Vec&,<T>);
  177.     a += <T>(4);
  178.     cout << NL << "a += 4:\n" << a <<NL;
  179. //friend    void        operator-=(<T>Vec&,const <T>Vec&);
  180.     a -= b;
  181.     cout << NL << "a -= b:\n" << a <<NL;
  182.  
  183. //friend    void        operator-=(<T>Vec&, <T>);
  184.     a -= <T>(4);
  185.     cout << NL << "a -= 4:\n" << a <<NL;
  186.  
  187. //friend    void        operator*=(<T>Vec&,const <T>Vec&);
  188.     a *= b;
  189.     cout << NL << "a *= b:\n" << a <<NL;
  190.  
  191. //friend    void        operator*=(<T>Vec&,<T>);
  192.     a *= <T>(2);
  193.     cout << NL << "a *= 2:\n" << a <<NL;
  194.  
  195. #if HAS_DIVIDE
  196. //friend    void        operator/=(<T>Vec&,const <T>Vec&);
  197.     a /= b;
  198.     cout << NL << "a /= b:\n" << a <<NL;
  199.     
  200. //friend    void        operator/=(<T>Vec&,<T>);
  201.     a /= <T>(2);
  202.     cout << NL << "a /= 2:\n" << a <<NL;
  203. #endif
  204.   }
  205.  
  206.   // math functions
  207.   {
  208.     cout << "\n**** Math functions ****\n";
  209. #if TYPE==Int_TYPE
  210. #define VALUE  1
  211. #else
  212. #define VALUE .5    
  213. #endif
  214.  
  215. #if TYPE==DComplex_TYPE
  216.     <T>Vec a(10, DComplex(-VALUE, 0));
  217.     <T>Vec b(10, DComplex(2.0, 2.0));
  218.     <T>Vec d(10, 0, DComplex(1,-1));
  219. #else
  220.     <T>Vec a(10, -VALUE);
  221.     <T>Vec b(10, <T>(2));
  222.     <T>Vec d(10, <T>(0), <T>(1));
  223. #endif
  224.  
  225.     <T>Vec c = a;
  226.     c.deepenShallowCopy();
  227.     c.slice(1,5,2) = <T>(VALUE);    // Should result in -.5, .5, -.5, etc.
  228.  
  229.     cout << NL << "a:\n" << a <<NL;
  230.     cout << NL << "b:\n" << b <<NL;
  231.     cout << NL << "c:\n" << c <<NL;
  232.     cout << NL << "d:\n" << d <<NL;
  233.  
  234. //friend    <T>Vec    abs(const <T>Vec& V);
  235.     cout << NL << "abs(a):\n" << abs(a) <<NL;
  236.  
  237. #if HAS_APPLY && TYPE!=DComplex_TYPE
  238. //friend    <T>Vec    acos(const <T>Vec& V)    { return V.apply(acos); }
  239.     cout << NL << "acos(a):\n" << acos(a) <<NL;
  240.  
  241. //friend    <T>Vec    asin(const <T>Vec& V)    { return V.apply(asin); }
  242.     cout << NL << "asin(a):\n" << asin(a) <<NL;
  243.  
  244. //friend    <T>Vec    atan(const <T>Vec& V)    { return V.apply(atan); }
  245.     cout << NL << "atan(a):\n" << atan(a) <<NL;
  246.  
  247. //friend    <T>Vec    atan2(const <T>Vec&,const <T>Vec&);
  248.     cout << NL << "atan2(a,c):\n" << atan2(a,c) <<NL;
  249.  
  250. //friend    <T>Vec    ceil(const <T>Vec& V)    { return V.apply(ceil); }
  251.     cout << NL << "ceil(c):\n" << ceil(c) <<NL;
  252. #endif
  253.  
  254. #if HAS_APPLY
  255. //friend    <T>Vec    cos(const <T>Vec& V)    { return V.apply(cos); }
  256.     cout << NL << "cos(a):\n" << cos(a) <<NL;
  257.  
  258. //friend    <T>Vec    cosh(const <T>Vec& V)    { return V.apply(cosh); }
  259.     cout << NL << "cosh(a):\n" << cosh(a) <<NL;
  260. #endif
  261.  
  262. //friend    <T>Vec    cumsum(const <T>Vec&);
  263.     cout << NL << "cumsum(a):\n" << cumsum(a) <<NL;
  264.  
  265. //friend    <T>Vec    delta(const <T>Vec&);
  266.     cout << NL << "delta(c):\n" << delta(c) << NL;
  267.     
  268. //friend    <T>        dot(const <T>Vec&,const <T>Vec&);
  269.     cout << NL << "dot(a,c):\n" << dot(a,c) << NL;
  270.  
  271. #if HAS_APPLY
  272. //friend    <T>Vec    floor(const <T>Vec& V)    { return V.apply(floor); }
  273.     cout << NL << "floor(c):\n" << floor(c) <<NL;
  274. #endif
  275.  
  276. #if HAS_MINMAX
  277. //friend    int        max(const <T>Vec&);
  278.     cout << NL << "max(d):\n" << max(d) << NL;
  279.  
  280. //friend    int        min(const <T>Vec&);
  281.     cout << NL << "min(d):\n" << min(d) << NL;
  282. #endif
  283.  
  284. #if HAS_MEAN
  285. //friend    <T>        mean(const <T>Vec&);
  286.     cout << NL << "mean(c):\n" << mean(c) << NL;
  287. #endif
  288.  
  289. //friend    <T>        prod(const <T>Vec&);
  290.     cout << NL << "prod(b):\n" << prod(b) << NL;
  291.  
  292. //friend    <T>Vec    reverse(const <T>Vec&);
  293.     cout << NL << "reverse(d):\n" << reverse(d) <<NL;
  294.  
  295. //friend    <T>        sum(const <T>Vec&);
  296.     cout << NL << "sum(c):\n" << sum(c) <<NL;
  297.  
  298. #if HAS_VARIANCE
  299. //friend        double        variance(const <T>Vec&);
  300.     cout << NL << "variance(c):\n" << variance(c) <<NL;
  301. #endif
  302.     
  303.   }
  304.   exit(0);
  305. }
  306.